#include "ConfigGui.hpp"
#include <string>

ConfigGui::ConfigGui(QString configFileName)
{
  this->edit = new QTextEdit(this);
  this->saveBtn = new QPushButton(tr("Save"), this);
  this->cancelBtn = new QPushButton(tr("Cancel"), this);

  connect(this->saveBtn, SIGNAL(clicked()),
          this, SLOT(save()));
  connect(this->cancelBtn, SIGNAL(clicked()),
          this, SLOT(close()));

  this->lay = new QVBoxLayout();
  this->btnLay = new QHBoxLayout();

  this->btnLay->addWidget(this->saveBtn);
  this->btnLay->addWidget(this->cancelBtn);
  this->lay->addWidget(this->edit);
  this->lay->addItem(this->btnLay);

  this->setLayout(this->lay);

  this->fileName = configFileName;
  this->read();
}

ConfigGui::~ConfigGui()
{
  delete this->edit;
  delete this->saveBtn;
  delete this->cancelBtn;
}

void ConfigGui::read()
{
  QFile *file = new QFile(this->fileName);

  file->open(QIODevice::ReadOnly);
  this->edit->clear();
  this->edit->append(file->readAll());
  file->close();

  delete file;
}

void ConfigGui::save()
{
  QFile *file = new QFile(this->fileName);

  file->open(QIODevice::WriteOnly);
  file->write(this->edit->toPlainText().toStdString().c_str());
  file->close();

  delete file;
  this->close();
}

